package com.axioma.dbmaint.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.axioma.dbmaint.datalayer.dao.DatabaseRepository;
import com.axioma.dbmaint.datalayer.dao.ServerRepository;
import com.axioma.dbmaint.datalayer.dto.DatabaseDTO;
import com.axioma.dbmaint.datalayer.dto.ServerDTO;
import com.axioma.dbmaint.model.Database;
import com.axioma.dbmaint.model.Server;
import com.axioma.dbmaint.service.ManagementService;

public class ManagementServiceImpl implements ManagementService {

   /* ServerRepository provides information and operations on known Axioma 7x database servers */
   ServerRepository serverDAO;
   
   /* DatabaseRepository provides information and operations on databases on a given server */
   DatabaseRepository databaseDAO;
   
   /* Spring will inject these for us. For testing these repos could be mocked */
   ManagementServiceImpl(ServerRepository pserverDAO, DatabaseRepository pdatabaseDAO) {
      serverDAO = pserverDAO;
      databaseDAO = pdatabaseDAO;
   }
   
   public List<Server> getServers() {
      List<ServerDTO> serverDTOs =  serverDAO.getServers();
      return mapServerDTOs(serverDTOs);
   }

   public List<Database> getDatabases(String serverName) {
      ServerDTO serverDTO = serverDAO.getServer(serverName);
      List<DatabaseDTO> dbDTOs = databaseDAO.getDatabasesOnServer(serverDTO);
      return mapDatabaseDTOs(dbDTOs);
   }

   public void createDatabase(String serverName, String databaseName,
         String schemaVersion) {
      // TODO Auto-generated method stub
      
   }

   public void resetDatabase(String serverName, String databaseName) {
      // TODO Auto-generated method stub
      
   }

   public void copyDatabase(String serverName, String sourceDatabaseName,
         String targetDatabaseName) {
      // TODO Auto-generated method stub
      
   }

   public List<Database> findDatabases(String schemaVersion) {
      // TODO Auto-generated method stub
      return null;
   }
   
   Server mapServerDTO(ServerDTO dto) {
      Server server = new Server();
      
      server.setServerName(dto.getServerName());
      server.setPort(dto.getPort());
      server.setMasterDatabaseName(dto.getMasterDatabaseName());
      server.setVendor(dto.getVendor().toString());
      server.setStatus(dto.getStatus().toString());
      server.setUser(dto.getUser());
      server.setPassword(dto.getPassword());
      
      return server;
   }
   
   List<Server> mapServerDTOs(List<ServerDTO> dtos) {
      List<Server> servers = new ArrayList<Server>();
      for (ServerDTO serverDTO : dtos) {
         servers.add(mapServerDTO(serverDTO));
      }
      return servers;
   }
   
   Database mapDatabaseDTO(DatabaseDTO dto) {
      Database db = new Database();
      db.setServerName(dto.getServerName());
      db.setDbName(dto.getDbName());
      db.setSchemaVersion(dto.getSchemaVersion());
      db.setSizeMB(dto.getSizeMB());
      db.setNumTables(dto.getNumTables());
      return db;
   }
   
   List<Database> mapDatabaseDTOs(List<DatabaseDTO> dtos) {
      List<Database> dbs = new ArrayList<Database>();
      if(dtos!=null){
	      for (DatabaseDTO databaseDTO : dtos) {
	         dbs.add(mapDatabaseDTO(databaseDTO));
	      }
      }
      return dbs;
   }
}
